package org.arestaurant;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;


import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Setting extends Activity{

    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting);
        Button saveBtn = (Button) findViewById(R.id.saveSetting);
        saveBtn.setClickable(false);
        EditText httpText1 = (EditText) findViewById(R.id.settingText1);
        if (getHttpSetting() != null) httpText1.setText(getHttpSetting());
        EditText httpText2 = (EditText) findViewById(R.id.settingText2);
        if (getHttpsSetting() != null) httpText2.setText(getHttpsSetting());
        saveBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                EditText httpText1 = (EditText) findViewById(R.id.settingText1);
                EditText httpText2 = (EditText) findViewById(R.id.settingText2);
                String httpStr = httpText1.getText().toString();
                String httpsStr = httpText2.getText().toString();
                ContextWrapper cw = new ContextWrapper(Main.CONTEXT);
                File directory = cw.getDir("sdcard", Context.MODE_PRIVATE);
                File settingFile = new File(directory.getPath() + "/setting.properties");
                try {
                    settingFile.delete();
                    settingFile.createNewFile();
                    Writer outW = new OutputStreamWriter(new FileOutputStream(settingFile));
                    PrintWriter out = new PrintWriter(outW);
                    out.println("HTTP_HOST=" + httpStr);
                    out.write("HTTPS_HOST=" + httpsStr);
                    out.close();
                    ConnectionServices.HTTP_CONNECTION_STATUS = false;
                    ConnectionServices.HTTP_HOST = null;
                    Intent intent = new Intent(Setting.this, Main.class);
                    finish();
                    startActivity(intent);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
             }
          });
        Button cancelBtn = (Button) findViewById(R.id.cancelSetting);
        cancelBtn.setOnClickListener(new View.OnClickListener() {
             public void onClick(View arg0) {
                 Intent intent = new Intent(Setting.this, Main.class);
                 startActivity(intent);
                 finish();
             }
          });
    }
    public static String getHttpSetting() {
        ContextWrapper cw = new ContextWrapper(Main.CONTEXT);
        File directory = cw.getDir("sdcard", Context.MODE_PRIVATE);
        File settingFile = new File(directory.getPath() + "/setting.properties");
        try {
            BufferedReader input =  new BufferedReader(new FileReader(settingFile));
            String line;
            while((line = input.readLine()) != null){
                if (line.contains("HTTP_HOST=")) {
                    return line.substring(10, line.length());
                }
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    public static String getHttpsSetting() {
        ContextWrapper cw = new ContextWrapper(Main.CONTEXT);
        File directory = cw.getDir("sdcard", Context.MODE_PRIVATE);
        File settingFile = new File(directory.getPath() + "/setting.properties");
        try {
            BufferedReader input =  new BufferedReader(new FileReader(settingFile));
            String line;
            while((line = input.readLine()) != null){
                if (line.contains("HTTPS_HOST=")) {
                    return line.substring(11, line.length());
                }
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}